﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace HRZoneTrainer
{
    public class Config
    {
        public Config()
        {

        }

        public static Config GetConfig()
        {
            if ( m_config == null )
            {
                try
                {
                    XmlSerializer xs = new XmlSerializer(typeof(Config));
                    FileStream fs = new FileStream(m_filePath, FileMode.Open, FileAccess.Read);

                    object cfgObj = xs.Deserialize(fs);

                    if ( cfgObj is Config )
                    {
                        m_config = cfgObj as Config;
                    }

                    fs.Close();
                }
                catch ( Exception exception )
                {
                    Trace.WriteLine(exception.Message);

                    m_config = new Config();
                }
            }
            return m_config;
        }

        [XmlElement("LastSessionWorkout")]
        public string LastSessionWorkout
        {
            get
            {
                return m_lastSessionWorkout;
            }
            set
            {
                m_lastSessionWorkout = value;
            }
        }

        [XmlElement("HRThreshold")]
        public int HRThreshold
        {
            get
            {
                return m_HRThreshold;
            }
            set
            {
                m_HRThreshold = value;
            }
        }

        [XmlElement("HRMaximum")]
        public int HRMaximum
        {
            get
            {
                return m_HRMaximum;
            }
            set
            {
                m_HRMaximum = value;
            }
        }

        [XmlElement("COMPort")]
        public string COMPort
        {
            get;
            set;
        }

        [XmlElement("AntiAliasing")]
        public bool AntiAliasing
        {
            get;
            set;
        }

        [XmlElement("FullScreen")]
        public bool FullScreen
        {
            get;
            set;
        }

        [XmlElement("Bounds")]
        public Rectangle Bounds
        {
            get;
            set;
        }

        [XmlElement("AutoSave")]
        public bool AutoSave
        {
            get;
            set;
        }

        [XmlElement("GraphType")]
        public HRZoneTrainer.GraphType GraphType
        {
            get;
            set;
        }

        [XmlElement("CurrentZoneSelection")]
        public string CurrentZoneSelection
        {
            get;
            set;
        }

        public static void Write()
        {
            if ( m_config != null )
            {
                try
                {
                    XmlSerializer writer =
                       new XmlSerializer(typeof(Config));
                    StreamWriter file =
                       new StreamWriter(m_filePath);

                    writer.Serialize(file, m_config);
                    file.Close();
                }
                catch ( Exception exception )
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }

        private static string m_filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Path.DirectorySeparatorChar + "HR Zone Trainer" + Path.DirectorySeparatorChar + "Config.xml";
        private int m_HRThreshold = 167;
        private int m_HRMaximum = 173;
        private string m_lastSessionWorkout = null;
        private static Config m_config = null; 
    }
}
